﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BackEnd.Common;
using BackEnd.Models;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Entities;
using BackEnd.Models;

namespace BackEnd.Controllers
{
    public class CountryController : Controller
    {
        //
        // GET: /ItemType/

        [CustomAuthorize(Action = Constants.AdminActions.ListCountry)]
        public ActionResult Index()
        {
            return View();
        }

        [CustomAuthorize(Action = Constants.AdminActions.CreateCountry)]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.CreateCountry)]
        public ActionResult Create(CountryModel countryModel)
        {
            if (string.IsNullOrWhiteSpace(countryModel.Country))
            {
                ModelState.AddModelError("Country", "Country is not null");
            }
            
            if (ModelState.IsValid == false)
            {
                return View();
            }
            else
            {
                new CountryBL().Insert(new Countrys { Country = countryModel.Country });
                return RedirectToAction("Index", "Country");
            }
        }

        [CustomAuthorize(Action = Constants.AdminActions.EditCountry)]
        public ActionResult Edit(int id)
        {
            CountryModel countryModel = new CountryModel();
            Countrys itemType = new CountryBL().GetById(id);
            countryModel.Country = itemType.Country;
            return View(countryModel);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.EditCountry)]
        public ActionResult Edit(CountryModel countryModel)
        {
            if (string.IsNullOrWhiteSpace(countryModel.Country))
            {
                ModelState.AddModelError("Country", "Country is not null");
            }

            if (ModelState.IsValid == false)
            {
                return View();
            }
            else
            {
                new CountryBL().Update(new Countrys { Id = countryModel.Id, Country = countryModel.Country });
                return RedirectToAction("Index", "Country");
            }
        }

        public JsonResult Search(string country, int pageIndex, int pageSize)
        {
            int totalRecord = 0;
            List<Countrys> listItemType = new CountryBL().Search(country, pageIndex, pageSize, out totalRecord);

            ListObjectModel<Countrys> listObject = new ListObjectModel<Countrys> { ListItem = listItemType, PageIndex = pageIndex, PageSize = pageSize, TotalRecord = totalRecord };
            return Json(listObject, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.DeleteCountry)]
        public JsonResult Delete(string listId)
        {            
            string[] arrayId = listId.Split('_');
            for (int i = 0; i < arrayId.Length; i++)
            {
                new CountryBL().Delete(Convert.ToInt32(arrayId[i]));                
            }
            return Json(1, JsonRequestBehavior.AllowGet);
        }

    }
}
